////////////////////////////////////////////////////////////////////////////////
//  COPYRIGHT (C) 2010 TELECOM DIGITAL MEDIA ("TDMEDIA").
//  ALL RIGHTS RESERVED.
//
//  THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
//  CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
//  MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
//  PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
package cc.telecomdigital.tdstock.activity.groups.foreignexchange;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.ForeignExchangeGroup;
import cc.telecomdigital.tdstock.adapter.CommonViewHolder;
import cc.telecomdigital.tdstock.adapter.ForeignExchangeRateOptionsAdapter;

/**
 * @author haibo for top bar button named "options"
 */
public class ForeignExchangeRateOptionsListActivity extends BaseActivity
{
	protected static final String	TAG			= ForeignExchangeRateOptionsListActivity.class.getSimpleName();
	private static final long		START_ID	= 90000l;

	private TextView[]				_txt;
	private Drawable[]				_sign;
	private ListView				_listView;
	private Button					_toolBtnReturn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.foreign_exchange_rate_options_list);

		_toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setFocusable(true);
		_toolBtnReturn.setClickable(true);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				TDStockLog.d(TAG, "close current activity and forwarding to the previous");

				Intent intent = new Intent(getApplicationContext(), ForeignExchangeActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);

				SwitchForwardActivity(ForeignExchangeActivity.class, intent);
			}
		});
		resetTitle();

		_listView = (ListView) findViewById(R.id.rate_options_list);
		final String[] txtRes = this.getResources().getStringArray(R.array.foreign_exchange_rate_options);
		int listSize = txtRes.length;
		_txt = new TextView[listSize];
		_sign = new Drawable[listSize];
		for (int index = 0; index < listSize; index++)
		{
			TextView title = new TextView(this);
			title.setText(txtRes[index]);
			_txt[index] = title;
			_sign[index] = this.getResources().getDrawable(
					R.drawable.go_next_icon);
		}

		List<CommonViewHolder> list = new ArrayList<CommonViewHolder>();
		for (int index = 0; index < listSize; index++)
		{
			long id = START_ID + index;
			CommonViewHolder viewHolder = new CommonViewHolder();
			viewHolder.id = id;
			viewHolder.text = _txt[index];
			viewHolder.sign = new ImageView(this);
			viewHolder.sign.setImageDrawable(_sign[index]);

			list.add(viewHolder);

		}
		_messageHandler = null; // this activity no need it.
		_listView.setAdapter(new ForeignExchangeRateOptionsAdapter(LayoutInflater.from(this), list));
		_listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				if(position < 0 || position > 7)
					return;
				
				((ITDLApplication) getApplicationContext()).putString(CommonDefn.TOP_BAR_TITLE, txtRes[position]);
				
				switch(position)
				{
					case 0:
						SwitchForwardActivity(ExchangeRateRMBActivity.class);
						break;
					case 1:
						SwitchForwardActivity(CurrencyExchangeActivity.class);
						break;
					case 3:
					case 4:
					case 5:
					case 6:
					case 7:
						EconomicCalendarContentActivity.iTypeId = (position - 3);
						SwitchForwardActivity(EconomicCalendarContentActivity.class);
						break;
				}
			}
		});
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		_listView.invalidateViews();
	}

	private void resetTitle()
	{
		if (((ITDLApplication) getApplicationContext()).getDataMode() == CommonDefn.DataMode.Delay)
		{
			_toolBtnReturn.setTextSize(12);
			_toolBtnReturn.setText(getResources().getString(R.string.foreign_exchange)
					+ "(" + getResources().getString(R.string.delay_mode_title_tips) + ")");
		}
		else
		{
			_toolBtnReturn.setText(getResources().getString(R.string.foreign_exchange));
		}
		_toolBtnReturn.postInvalidate();
	}

	@Override
	public ActivityGroupController GetActivityGroupController()
	{
		return ForeignExchangeGroup.GetActivityGroupHostControl();
	}

}
